﻿using System;
using System.Data;
using System.Data.SqlTypes;
using System.IO;
using System.Text;
using HelpdeskServices.CrmWebReference;
using HelpdeskServices.KmsWebReference;
using HelpdeskServices.Properties;
using HelpdeskServices.IntegrationServices;

public class WebCase : ICloneable, INullable
{
    #region Fields

    private string m_ConfirmationRequest = "";
    private string m_ConfirmationSentDate = "";
    private string m_CrmSentDate = "";
    private string m_CustomerFeedbackDate = "";
    private int m_CustomerFeedbackID = 0;
    private string m_EmailAddressOnWebCase = "";
    private int m_FeedbackCategoryID = 0;
    private string m_FeedbackCategoryList = "";
    private string m_FeedbackMessage = "";
    private string m_FirstName = "";
    private bool m_hasBeenFoundInCrm = false;
    private string m_HttpUserAgent = "";
    private string m_LanguagePreferance = "";
    private string m_LastName = "";
    private string m_ShopperId = "";
    private int m_ShortOrderId = 0;
    private Lookup m_Subject;

    #endregion Fields

    #region Properties

    public int CustomerFeedbackID
    {
        get { return m_CustomerFeedbackID; }
    }

    public string ConfirmationRequest
    {
        get { return m_ConfirmationRequest; }
    }

    public string ConfirmationSentDate
    {
        get { return m_ConfirmationSentDate; }
    }

    public string CrmSentDate
    {
        get { return m_CrmSentDate; }
    }

    public string CustomerFeedbackDate
    {
        get { return m_CustomerFeedbackDate; }
    }

    public int FeedbackCategoryID
    {
        get { return m_FeedbackCategoryID; }
    }

    public string FeedbackCategoryList
    {
        get { return m_FeedbackCategoryList; }
    }

    public string FeedbackMessage
    {
        get { return m_FeedbackMessage; }
    }

    public string ShopperId
    {
        get { return m_ShopperId; }
    }

    public string HttpUserAgent
    {
        get { return m_HttpUserAgent; }
    }

    public string FirstName
    {
        get { return m_FirstName; }
    }

    public string LastName
    {
        get { return m_LastName; }
    }

    public string LanguagePreferance
    {
        get { return m_LanguagePreferance; }
    }

    public string Email
    {
        get { return m_EmailAddressOnWebCase; }
    }

    public int ShortOrderId
    {
        get { return m_ShortOrderId; }
    }

    #endregion

    public WebCase(CustomerFeedback webcase)
    {
        setLocalParameters(webcase);
    }

    public WebCase()
    {
    }

    public bool HasBeenFoundInCrm
    {
        get { return m_hasBeenFoundInCrm; }
        set { m_hasBeenFoundInCrm = value; }
    }

    #region ICloneable Members

    public object Clone()
    {
        return this;
    }

    #endregion

    #region INullable Members

    public virtual bool IsNull
    {
        get { return false; }
    }

    public Lookup Subject
    {
        get 
        { 
            if(m_Subject == null)
            {
                getSubject();
            }
            return m_Subject;
        }
        set { m_Subject = value; }
    }

    #endregion

    public void Parse()
    {
        Validate();
    }

    private void setLocalParameters(CustomerFeedback webcase)
    {
        int.TryParse(webcase.CustomerFeedbackID, out m_CustomerFeedbackID);
        m_CustomerFeedbackDate = webcase.CustomerFeedbackDate;
        m_ShopperId = webcase.ShopperId;
        m_HttpUserAgent = webcase.HttpUserAgent;
        m_FirstName = webcase.FirstName;
        m_LastName = webcase.LastName;
        m_LanguagePreferance = webcase.LanguagePref;
        m_EmailAddressOnWebCase = webcase.Email;
        int.TryParse(webcase.FeedbackCategoryID, out m_FeedbackCategoryID);
        m_FeedbackCategoryList = webcase.FeedbackCategoryList;
        int.TryParse(webcase.ShortOrderID, out m_ShortOrderId);
        m_FeedbackMessage = webcase.FeedbackMessage;
        m_ConfirmationRequest = webcase.ConfirmationRequest;
        m_ConfirmationSentDate = webcase.ConfirmationSentDate;
        m_CrmSentDate = webcase.CRMSentDate;
    }

    internal virtual void Validate()
    {
        CrmService crmService = Common.CrmWebServiceProxy();

        string results = crmService.Fetch(fetchWebCaseSearch());
        if (results == Resources.NoResults)
        {
            m_hasBeenFoundInCrm = false;
        }
        else
        {
            m_hasBeenFoundInCrm = true;
        }
    }

    internal virtual void getSubject()
    {
        CrmService crmService = Common.CrmWebServiceProxy();
        string results = crmService.Fetch(fetchSubjectSearch());
        if(results!= Resources.NoResults)
        {
            Guid subjectId = new Guid(ConvertQueryResults(results).Rows[0].ItemArray[0].ToString());
            m_Subject = CrmTypes.CreateLookup(EntityName.subject.ToString(), subjectId);
        }
        else
        {
            Guid subjectId = new Guid(System.Configuration.ConfigurationManager.AppSettings["ImportWebCaseSubjectId"].ToString());
            m_Subject = CrmTypes.CreateLookup(EntityName.subject.ToString(), subjectId);
        }
    }
    private static DataTable ConvertQueryResults(string results)
    {

        DataSet ds = new DataSet();
        DataTable dataTable = new DataTable();
        StringReader reader = new StringReader(results);
        ds.ReadXml(reader);
        if(ds.Tables.Contains("result"))
        {
            return ds.Tables["result"];
        }
        else
        {
            return ds.Tables[0];
        }
    }
    private string fetchSubjectSearch()
    {
        StringBuilder subjectSearch = new StringBuilder();

        if(System.Configuration.ConfigurationManager.AppSettings["CleverMatchingWebCaseSubjectsOn"].ToLower().Equals("true"))
        {
            subjectSearch.AppendFormat(Resources.XmlFetchSearchSubjectOnTitle.ToString(), m_FeedbackCategoryList);
        }
        else
        {
            subjectSearch.AppendFormat(Resources.XmlFetchSearchSubject.ToString(), m_FeedbackCategoryID.ToString());
        }
        return subjectSearch.ToString();
    }
    private string fetchWebCaseSearch()
    {
        StringBuilder fetchQuery = new StringBuilder();
        fetchQuery.AppendFormat(Resources.XmlFetchDuplicateWebCaseImportPreventingQuery.ToString(),
                                m_CustomerFeedbackID.ToString());
        return fetchQuery.ToString();
    }
}